package com.donfu.web.controller.wxhouse;

import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.core.page.TableDataInfo;
import com.donfu.common.enums.BusinessType;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.system.domain.house.HouseCollection;
import com.donfu.system.domain.house.HouseComment;
import com.donfu.system.domain.house.HouseDiary;
import com.donfu.system.service.house.IHouseCollectionService;
import com.donfu.system.service.house.IHouseDiaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.Date;
import java.util.List;

/**
 * 新居日志Controller
 *
 * @author mo
 * @date 2021-12-01
 */
@Api(tags = "前台-新居日志")
@RestController
@RequestMapping("/wx/house/diary")
public class WebHouseDiaryController extends BaseController {
    @Autowired
    private IHouseDiaryService houseDiaryService;
    @Autowired
    private IHouseCollectionService houseCollectionService;

    /**
     * 查询新居日志列表
     */
//    @PreAuthorize("@ss.hasPermi('house:diary:list')")
    @ApiOperation("列表查询")
    @GetMapping("/list")
    public TableDataInfo list(HouseDiary houseDiary,@RequestParam Integer userId) {
        startPage();
        if (userId != null) {
            List<HouseDiary> list = houseDiaryService.selectHouseDiaryList(houseDiary);
            return getDataTable(list);
        }
        return null;
    }

    /**
     * 获取新居日志详细信息
     */
//    @PreAuthorize("@ss.hasPermi('house:diary:query')")
    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable(value = "id") Long id, Long userId) {
        if (userId != null) {
            HouseDiary houseDiary = houseDiaryService.selectHouseDiaryById(id);
            if (houseDiary != null && houseDiary.getContent() != null) {
                //解析富文本内容
                houseDiary.setContent(HtmlUtils.htmlUnescape(houseDiary.getContent()));
                //查询收藏表
                HouseCollection collection = new HouseCollection();
                collection.setType(3);
                collection.setValueId(id);
                collection.setUserId(userId);
                List<HouseCollection> houseCollections = houseCollectionService.selectHouseCollectionList(collection);
                if (houseCollections.size() > 0) {
                    //已收藏
                    houseDiary.setCollStatus(1);
                } else {
                    //未收藏
                    houseDiary.setCollStatus(0);
                }
            }
            return AjaxResult.success(houseDiary);
        }
        return AjaxResult.error("请先登录");
    }


    @PostMapping("/collection")
    public AjaxResult collection(@RequestBody HouseDiary houseDiary) {
        HouseCollection houseCollection = new HouseCollection();
        if (houseDiary.getUserId() != null) {
            if (houseDiary != null && houseDiary.getCollStatus() != null && houseDiary.getId() != null) {
                if (houseDiary.getCollStatus() == 0) {
                    //收藏
                    houseCollection.setUserId(houseDiary.getUserId());
                    houseCollection.setType(3);//类型 1产品 2客人之家 3新居日志
                    houseCollection.setValueId(houseDiary.getId());
                    houseCollection.setCreateDate(new Date());
                    int result = houseCollectionService.insertHouseCollection(houseCollection);
                    if (result == 1) {
                        return AjaxResult.success("收藏成功");
                    }
                } else {
                    //取消收藏，获取收藏id
                    HouseCollection collection = new HouseCollection();
                    collection.setUserId(houseDiary.getUserId());
                    collection.setType(3);//类型 1产品 2客人之家 3新居日志
                    collection.setValueId(houseDiary.getId());
                    List<HouseCollection> houseCollections = houseCollectionService.selectHouseCollectionList(collection);
                    if (houseCollections.size() > 0) {
                        int i = houseCollectionService.deleteHouseCollectionById(houseCollections.get(0).getId());
                        if (i != 0) {
                            return AjaxResult.success("取消收藏成功");
                        }
                    } else {
                        return AjaxResult.error("取消收藏失败");
                    }
                }
            }
            return AjaxResult.error("收藏失败");
        }
        return AjaxResult.error("请先登录");
    }
}
